# -*-coding:utf-8-*-


import websocket
import time
from ctypes import *
import requests
from bs4 import BeautifulSoup
import multiprocessing
import re,threading
import json
import os,sys

#引入dll
add_path = os.path.split(os.path.abspath(__file__))[0]+'\\'
#os.add_dll_directory('.\\')
dd_dll = CDLL(add_path + 'DD94687.dll')

timeValue = 1
countValue = 5
count = 1
zhiling = ['#上', '#上3', '#下', '#下3', '#左', '#左3', '#右', '#右3', '#确认', '#返回', '#保存游戏', '#查看背包', '#查看技能', '#跳过']
SendInput = windll.user32.SendInput
# 关键键值描述
keyCodeDec = {'backspace': 0x08, 'tab': 0x09, 'enter': 0x0D, 'esc': 0x1B, 'left_arrow': 0x25,
              'up_arrow': 0x26, 'right_arrow': 0x27, 'down_arrow': 0x28,
              '0': 0x30, '1': 0x31, '2': 0x32, '3': 0x33, '4': 0x34, '5': 0x35, '6': 0x36, '7': 0x37, '8': 0x38,
              '9': 0x39,'a': 0x41, 'b': 0x42, 'c': 0x43, 'd': 0x44, 'e': 0x45, 'f': 0x46, 'g': 0x47, 'h': 0x48, 'i': 0x49,
              'j': 0x4A,'k': 0x4B, 'l': 0x4C, 'm': 0x4D, 'n': 0x4E, 'o': 0x4F, 'p': 0x50, 'q': 0x51, 'r': 0x52, 's': 0x53,
              't': 0x54,'u': 0x55, 'v': 0x56, 'w': 0x57, 'x': 0x58, 'y': 0x59, 'z': 0x5A}
# 关键键 十六进制
keyCodeHex = {0x08: 0x0E, 0x09: 0x0F, 0x0D: 0x1C, 0x1B: 0x01,
              0x25: 0x4B, 0x26: 0x48, 0x27: 0x4D, 0x28: 0x50, 0x30: 0x0B, 0x31: 0x02, 0x32: 0x03, 0x33: 0x04,
              0x34: 0x05, 0x35: 0x06, 0x36: 0x07, 0x37: 0x08,
              0x38: 0x09, 0x39: 0x0A, 0x41: 0x1E, 0x42: 0x30, 0x43: 0x2E, 0x44: 0x20, 0x45: 0x12, 0x46: 0x21,
              0x47: 0x22, 0x48: 0x23, 0x49: 0x17, 0x4A: 0x24,
              0x4B: 0x25, 0x4C: 0x26, 0x4D: 0x32, 0x4E: 0x31, 0x4F: 0x18, 0x50: 0x19, 0x51: 0x10, 0x52: 0x13,
              0x53: 0x1F, 0x54: 0x14, 0x55: 0x16, 0x56: 0x2F,
              0x57: 0x11, 0x58: 0x2D, 0x59: 0x15, 0x5A: 0x2C}
# C struct redefinitions

PUL = POINTER(c_ulong)
#键盘输入
class KeyBdInput(Structure):
    _fields_ = [("wVk", c_ushort),
                ("wScan", c_ushort),
                ("dwFlags", c_ulong),
                ("time", c_ulong),
                ("dwExtraInfo", PUL)]
#硬件设备输入
class HardwareInput(Structure):
    _fields_ = [("uMsg", c_ulong),
                ("wParamL", c_short),
                ("wParamH", c_ushort)]
#鼠标输入
class MouseInput(Structure):
    _fields_ = [("dx", c_long),
                ("dy", c_long),
                ("mouseData", c_ulong),
                ("dwFlags", c_ulong),
                ("time", c_ulong),
                ("dwExtraInfo", PUL)]

class Input_I(Union):
    _fields_ = [("ki", KeyBdInput),
                ("mi", MouseInput),
                ("hi", HardwareInput)]

class Input(Structure):
    _fields_ = [("type", c_ulong),
                ("ii", Input_I)]

# Actuals Functions

def PressKey(hexKeyCode):
    extra = c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput(hexKeyCode, keyCodeHex[hexKeyCode], 0, 0, pointer(extra))
    x = Input(c_ulong(1), ii_)
    windll.user32.SendInput(1, pointer(x), sizeof(x))


def ReleaseKey(hexKeyCode):
    extra = c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput(hexKeyCode, keyCodeHex[hexKeyCode], 0x0002, 0, pointer(extra))
    x = Input(c_ulong(1), ii_)
    windll.user32.SendInput(1, pointer(x), sizeof(x))


def sendInput(s_input):
    if (s_input in keyCodeDec.keys()):
        ##        if(keyCodeDec[s_input] == 'enter' or keyCodeDec[s_input] == 'esc'):
        ##            k = 0.1
        ##        else:
        k = 0.1
        PressKey(keyCodeDec[s_input])
        time.sleep(k)
        ReleaseKey(keyCodeDec[s_input])


# directx scan codes http://www.gamespp.com/directx/directInputKeyboardScanCodes.html
def LookMyStatus():  # 查看自己的状态
    sendInput('s')


def UseSkill():  # 使用技能
    sendInput('esc')
    time.sleep(0.05)
    sendInput('down_arrow')
    time.sleep(0.05)
    sendInput('enter')


def LookBag():  # 查看我的背包
    sendInput('esc')
    time.sleep(0.05)
    sendInput('down_arrow')
    time.sleep(0.05)
    sendInput('down_arrow')
    time.sleep(0.05)
    sendInput('enter')


def SaveGame():  # 保存游戏
    sendInput('esc')
    time.sleep(0.05)
    sendInput('up_arrow')
    time.sleep(0.05)
    sendInput('enter')
    time.sleep(0.05)
    sendInput('enter')
    time.sleep(0.05)
    sendInput('enter')


def Enter():
    sendInput('enter')


def Up():
    sendInput('up_arrow')


def Down():
    sendInput('down_arrow')


def Right():
    sendInput('right_arrow')


def Left():
    sendInput('left_arrow')


def average(what):
    if (what == 'saveGame'):
        SaveGame()
    elif (what == 'lookBag'):
        LookBag()
    elif (what == 'useSkill'):
        UseSkill()
    elif (what == 'tiaoGuo'):
        for i in range(8):
            sendInput('r')
    else:
        sendInput(what)


def Move(i):
    if (i == '#w'):
        Up()
    elif (i == '#s'):
        Down()
    elif (i == '#a'):
        Left()
    elif (i == '#d'):
        Right()


def chuli_1():
    global dic
    move = re.compile(r'#[wasd]([1-9][0-9]?)?')
    while (True):
        if (dic != {}):
            print('dic = ', dic)
            # time.sleep(2)
        for i in list(dic.values()):
            pp = move.match(i)
            if (pp == None):  # 不是方向键
                # 处理其他指令和错误指令
                if (i == '#c'):  # 保存游戏
                    SaveGame()
                elif (i == '#b'):  # 查看背包
                    LookBag()
                elif (i == '#k'):  # 使用技能
                    UseSkill()
                elif (i == '#t'):  # 跳过对话
                    average('tiaoGuo')
                elif (i == '#h'):
                    average('enter')
                elif (i == '#e'):
                    average('esc')
                elif (i == '#A'):  # 战斗状态下，自动普通攻击直至结束
                    average('a')
                elif (i == '#D'):  # 战斗状态下，防御
                    average('d')
                else:  # 非法指令
                    continue
            else:
                s = pp.group()
                totalCount = re.sub("\D", "", s)
                if (totalCount == ''):  # 单纯移动
                    Move(s)
                else:  # 移动加数字
                    m = re.compile(r'(#[wasd])')
                    m = m.match(s)
                    for j in range(int(totalCount)):
                        Move(m.group())
        dic = {}
        time.sleep(0.5)


def chuli():
    global dic
    global timeValue
    global countValue
    global zhiling
    global count
    while (True):
        up_arrow = 0
        down_arrow = 0
        left_arrow = 0
        right_arrow = 0
        enter = 0
        esc = 0
        saveGame = 0
        lookBag = 0
        useSkill = 0
        tiaoGuo = 0
        for i in range(timeValue):  # 每隔timeValue秒计算一次
            time.sleep(1)
            if (len(dic) >= countValue):  # 如果此时消息数大于countValue了，跳出去
                break
        # 5秒后

        d = {}
        # 去掉无关指令
        s = [i for i in dic.keys() if (dic[i] not in zhiling)]  # 找出所有不在指令内的操作
        if (s != []):
            print('无关指令 :', s)
            for i in s:
                dic.pop(i)
        # print('dic为：',dic)
        for i in list(dic.values()):
            if (i == '#上'):
                up_arrow += 1
            elif (i == '#下'):
                down_arrow += 1
            elif (i == '#左'):
                left_arrow += 1
            elif (i == '#右'):
                right_arrow += 1
            elif (i == '#确认'):
                enter += 1
            elif (i == '#返回'):
                esc += 1
            elif (i == '#保存游戏'):
                saveGame += 1
            elif (i == '#查看背包'):
                lookBag += 1
            elif (i == '#查看技能'):
                useSkill += 1
            elif (i == '#跳过'):
                tiaoGuo += 1
                # 保存游戏','#查看背包','#查看技能
        if (up_arrow != 0):
            d['up_arrow'] = up_arrow
        if (down_arrow != 0):
            d['down_arrow'] = down_arrow
        if (left_arrow != 0):
            d['left_arrow'] = left_arrow
        if (right_arrow != 0):
            d['right_arrow'] = right_arrow
        if (enter != 0):
            d['enter'] = enter
        if (esc != 0):
            d['esc'] = esc
        if (saveGame != 0):
            d['saveGame'] = saveGame
        if (lookBag != 0):
            d['lookBag'] = lookBag
        if (useSkill != 0):
            d['useSkill'] = useSkill
        if (tiaoGuo != 0):
            d['tiaoGuo'] = tiaoGuo
        if (d != {}):
            print('最终操作为：', max(d))
            average(max(d))
            dic = {}
#使用dll
def chuli_2():
    global dic
    global timeValue
    global countValue
    global zhiling
    global count
    while (True):
        up_arrow = 0
        down_arrow = 0
        left_arrow = 0
        right_arrow = 0
        enter = 0
        esc = 0
        saveGame = 0
        lookBag = 0
        useSkill = 0
        tiaoGuo = 0
        for i in range(timeValue):  # 每隔timeValue秒计算一次
            time.sleep(1)
        # 5秒后

        d = {}
        # 去掉无关指令
        s = [i for i in dic.keys() if (dic[i] not in zhiling)]  # 找出所有不在指令内的操作
        if (s != []):
            print('无关指令 :', s)
            for i in s:
                dic.pop(i)
        # print('dic为：',dic)
        for i in list(dic.values()):
            if (i == '#上'):
                up_arrow += 1
            elif (i == '#下'):
                down_arrow += 1
            elif (i == '#左'):
                left_arrow += 1
            elif (i == '#右'):
                right_arrow += 1
            elif (i == '#确认'):
                enter += 1
            elif (i == '#返回'):
                esc += 1
            elif (i == '#保存游戏'):
                saveGame += 1
            elif (i == '#查看背包'):
                lookBag += 1
            elif (i == '#查看技能'):
                useSkill += 1
            elif (i == '#跳过'):
                tiaoGuo += 1
                # 保存游戏','#查看背包','#查看技能
        if (up_arrow != 0):
            d['up_arrow'] = up_arrow
        if (down_arrow != 0):
            d['down_arrow'] = down_arrow
        if (left_arrow != 0):
            d['left_arrow'] = left_arrow
        if (right_arrow != 0):
            d['right_arrow'] = right_arrow
        if (enter != 0):
            d['enter'] = enter
        if (esc != 0):
            d['esc'] = esc
        if (saveGame != 0):
            d['saveGame'] = saveGame
        if (lookBag != 0):
            d['lookBag'] = lookBag
        if (useSkill != 0):
            d['useSkill'] = useSkill
        if (tiaoGuo != 0):
            d['tiaoGuo'] = tiaoGuo
        if (d != {}):
            print('最终操作为：', max(d))
            average(max(d))
            dic = {}



def on_message(ws, message):
    global dic
    # print(ws)
    allMsg = json.loads(message)
    dic[allMsg['UserId']] = '#'+allMsg['Content']

def on_error(ws, error):
    # print(ws)
    print(error)


def on_close(ws):
    # print(ws)
    print("### closed ###")


# 打印日志
# websocket.enableTrace(True)
headers = {'roomid': '453523442361'}
ws = websocket.WebSocketApp("ws://127.0.0.1:8001",
                            header=headers,
                            on_message=on_message,
                            on_error=on_error,
                            on_close=on_close)
dic = {}

p1 = threading.Thread(target=chuli_1)
p1.daemon=False
p1.start()
ws.run_forever()
